package com.example.mapsapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;

import com.example.mapsapp.Login.UserLoginTask;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


public class SearchPlaces extends Activity implements LocationListener,View.OnClickListener {
    public static GoogleMap mapes;
    public static double longitude =0.0;
    public static double latitude=0.0;
	public static ArrayList<Place> nana;
	private GetPlacesTask mAuthTask = null;
	public static Context cont;
	public String info;
	EditText edt;
	private static final long MIN_TIME = 400;
	private static final float MIN_DISTANCE = 1000;
	public LocationManager locationManager;
	public static LatLng lati;
	int radius = 900;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_places);
		 mapes = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
		 edt = (EditText)findViewById(R.id.editText1);
		 Button map = (Button) findViewById(R.id.findFavorite);
	     map.setOnClickListener(this);
		 getLocation();
		 Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		 longitude = location.getLongitude();
		 latitude = location.getLatitude();
		 lati = new LatLng( location.getLatitude(),location.getLongitude());
		 mapes.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).position(lati).title(Login.user + "'s Location"));
		
	     
	     
	
		  
	}
	 @Override
	    public void onClick(View v){

		 		switch (v.getId()){
		 		case R.id.findFavorite:
		 			System.out.println("i am here");
		 			info = edt.getText().toString();
		 			mAuthTask = new GetPlacesTask();
		 			mAuthTask.execute(info);
		 			 System.out.println("i am here1");
		 			  			 
		 				break;
		 				
		 		}
		 	}

	
	 public void getLocation(){
		  locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		  locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
	  }
		
	@Override
	public void onLocationChanged(Location location) {
		LatLng latLng1 = new LatLng(location.getLatitude(), location.getLongitude());
	    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng1, 16);
	    mapes.animateCamera(cameraUpdate);
	    locationManager.removeUpdates(this);
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
	public void getPlace(){
		info = edt.getText().toString();
	}
	
 
	
	protected void onPostExecute(ArrayList<Place> result)
	{
		
  
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.search_places, menu);
	    return true;
	}
		
	 public boolean onOptionsItemSelected(MenuItem item) {
		    switch(item.getItemId())
		    {
		    case R.id.locationOption:
		    	setRadius();
				return true;
			}
			return true;
		}
	 
	 public void setRadius(){
		 
		 AlertDialog.Builder altDialog= new AlertDialog.Builder((findViewById(R.id.map).getContext()));
		 //altDialog.setTitle("Radius Is Set To: " + radius); 
		 altDialog.setMessage("Set Radius: ");
		 altDialog.setNeutralButton("OK", new DialogInterface.OnClickListener() {
		    
		    @Override
		    public void onClick(DialogInterface dialog, int which) {
		     // TODO Auto-generated method stub
		     Toast.makeText(getApplicationContext(), "Ok button Clicked ", Toast.LENGTH_LONG).show();
		    }
		   });
		    final EditText input = new EditText(this);
			altDialog.setView(input);

			AlertDialog alertDialog = altDialog.create();
			alertDialog.show();

		 }
	}
		 
		 
	  
	 

